def prost(n):
   if n == 1:
       return False
   a = 0
   for i in range(2, int(n ** 0.5) + 1):
       if (n % i == 0):
           a = a + 1
   if (a == 0):
       return True
   else:
       return False


def generate(a):
   a = str(a)
   results = []
   for i in range(len(a)):
       for j in range(i, len(a)):
           results.append(a[i:j+1])
   return(results)


x = int(input())
if x < 1000 or x > 9999:
   print(0)
   exit()
sp = generate(x)
sp = [int(x) for x in sp]
sp.sort(reverse=True)
for x in sp:
   if prost(x):
       print(x)
       break
else:
   print(0)
